// pages/manage/manage.js
var util = require('../../utils/util.js')
//
const DB = wx.cloud.database()
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户身份信息 
    openid: wx.getStorageSync('openid'),
    
    nickname: "",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // nickname: userInfo.nickName,
    //导航栏处理 
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navbarData: {
      showCapsule: 0,
      title: "懒小丫图书管理"
    },

    //我的借阅数据
    DB_my: {}

  },

  //开通权限
  register() {
    var that = this
    
    wx.getUserInfo({
      success: function (e) {
        that.setData({
          userInfo: e.userInfo,
          hasUserInfo: true,
          nickname: e.userInfo.nickName
        })
        DB.collection("lchuser").where({
          openid: that.data.openid
        }).get({
          success: function (e) {
            let id = e.data[0]
            if (id == undefined) {
              DB.collection("lchuser").add({
                data: {
                  openid: that.data.openid,
                  nickname: that.data.nickname,
                  register: 0,
                  manger: 0
                },
                success: function () {
                  wx.showToast({
                    title: '提交成功',
                    duration: 2000,
                    image: '/images/adduser.png'
                  })
                  console.log("222", id)
                },
                fail: function () {
                  wx.showToast({
                    title: '服务器故障',
                    duration: 2000,
                    image: "/images/fail.png"
                  })
                }
              })
            } else {
              wx.showToast({
                title: '请等待审核',
                image: '/images/fail.png',
                duration: 2000
              })
            }
          }
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '未获取用户信息',
          image: '/images/fail.png',
          duration: 2000
        })
      }
    })


  },

  //还书处理
  huanshu(e) {
    setTimeout(() => {
    let that = this
    let id = e.currentTarget.dataset.id
    let stock = e.currentTarget.dataset.stock
    let jynum = e.currentTarget.dataset.jynum
    wx.showModal({
      title: '懒小丫提醒',
      content: '你确认要还这本书吗？',
      success(res) {
        if (res.confirm) {
          DB.collection("lchbook").doc(id).update({
            data: {
              hsstate: true,
            },
            success: function () {
              wx.showToast({
                title: '还书成功',
                duration: 2000,
                icon: 'success'
              })
              DB.collection("lchbook").where({
                openid: that.data.openid
              }).get().then(res => {
                that.setData({
                  DB_my: res.data
                })
              })
            },
            fail: (res) => {
              wx.showToast({
                title: '数据库失败 ',
                duration: 2000,
                image: '/images/fail.png'
              })
            }
          })
        }
      }
    })

    }, 1000);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    //获取头像相关信息，是否显示按钮 
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        nickname: app.globalData.userInfo.nickName
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          nickname: app.globalData.userInfo.nickName
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            nickname: res.userInfo.nickName
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //tabbar显示
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        tabIndex: wx.getStorageSync('tabIndex')
      })
    }

    DB.collection("lchbook").orderBy('jydate' ,  'desc').where({
      openid: this.data.openid
    }).get().then(res => {
      this.setData({
        DB_my: res.data
      })
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getUserInfo: function (e) {
   let that = this
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    wx.cloud.callFunction({
      name: 'openid',
      complete: res => {
        let openid =  res.result.openid
        wx.setStorageSync('openid', openid)
        that.setData({openid: openid})
      }
    })
  }
})